<?php
include('rootlogin.php');
include "../../Classes\PHPExcel.php";
//include('../../incsession.php');
session_start();
$level=2;
$num1=$_POST['num1'];	
$i=1;
while($i<=$num1){
	$num2[$i]=$_POST['num2_'.$i];	
	$DND2[$i]=$_POST['DND2_'.$i];
	$j=1;
	while($j<=$num2[$i]+$DND2[$i]){
		$num3[$i][$j]=$_POST['num3_'.$i.'_'.$j];
		$DND3[$i][$j]=$_POST['DND3_'.$i.'_'.$j];
		$j++;
	}
	$i++;
}
$DND1=$_POST['DND1'];
for($i=$num1+1; $i<=$DND1+$num1; $i++){
			$DND2[$i]=$_POST['DND2_'.$i];
			for($j=1; $j<=$DND2[$i]; $j++){
				$DND3[$i][$j]=$_POST['DND3_'.$i.'_'.$j];
			}
}
$fake_num=$_POST['fake_num'];
$FAKE=$_POST['FAKE'];
//add to database and leave
if($_POST['submit']=="submit"){
	// Export xls
	/** Error reporting */ 
	error_reporting(E_ALL); 	
	/** PHPExcel */ 
	require_once "../../Classes\PHPExcel.php"; 	
	/** PHPExcel_IOFactory */ 
	require_once "../../Classes\PHPExcel\IOFactory.php";
	$objPHPExcel = new PHPExcel(); 
	$objPHPExcel->setActiveSheetIndex(0);
	
	$objPHPExcel->getActiveSheet()->getStyle("A1")->getFont()->setSize(16);
	$objPHPExcel->getActiveSheet()->setCellValue("A1","題目敘述");
	$objPHPExcel->getActiveSheet()->getStyle("B1")->getFont()->setSize(16);
	$objPHPExcel->getActiveSheet()->setCellValue("B1","正確答案");
	$excel_row=0;
	
	$i=1;
	$m=1;
	
	
	while($i <= $num1)
	{	
		if($_POST['Q'.$i]=='' && $_POST['answer'.$i]==''){
			//delete---------------------------------------------------------------
			$oldlayer=$i*10000;		
			$query = "DELETE FROM drag_and_drop   
					WHERE layer=".$oldlayer." AND topicid=".$_SESSION['topic']." AND level=".$level;
			$result = mysql_query($query,$user_con);
			$query = "DELETE FROM dnd_answer 
						WHERE layer=".$oldlayer." ADN topicid=".$_SESSION['topic']." AND level=".$level;
			$result = mysql_query($query,$user_con);
		}
		if($_POST['Q'.$i]!='' && $_POST['Q'.$i]!=' '){
			$layer=$m*10000;
			$oldlayer=$i*10000;	
			$Q = str_replace("'", "&#39;", $_POST['Q'.$i]);
			$Q = str_replace('"', '&quot;', $Q);
			$A = str_replace("'", "&#39;", $_POST['answer'.$i]);
			$A = str_replace('"', '&quot;', $A);
			$query = "UPDATE drag_and_drop  
					SET question='".$Q."',
						layer='".$layer."' 
					WHERE layer=".$oldlayer." AND topicid=".$_SESSION['topic']." AND level=".$level;
			$result = mysql_query($query,$user_con);
			$query = "UPDATE dnd_answer 
					SET answer='".$A."',
						layer='".$layer."' 
						WHERE layer=".$oldlayer." ADN topicid=".$_SESSION['topic']." AND level=".$level;
			$result = mysql_query($query,$user_con);
			
			//excel
			$objPHPExcel->getActiveSheet()->getStyle("A".($excel_row+2))->getFont()->setSize(16);
			$objPHPExcel->getActiveSheet()->getStyle("A".($excel_row+2))->applyFromArray( 
			array("fill"     => array("type"        => PHPExcel_Style_Fill::FILL_SOLID, 
										"color"        => array("rgb" => "ADDFFF")), ) );
			$Q = str_replace("&#39;", "'", $Q);
			$Q = str_replace('&quot;', '"', $Q);
			$objPHPExcel->getActiveSheet()->setCellValue("A".($excel_row+2)," ".$Q);
			
			$A = str_replace("&#39;", "'", $A);
			$A = str_replace('&quot;', '"', $A);
			$objPHPExcel->getActiveSheet()->getStyle("B".($excel_row+2))->getFont()->setSize(14);
			$objPHPExcel->getActiveSheet()->getStyle("B".($excel_row+2))->applyFromArray( 
			array("fill"     => array("type"        => PHPExcel_Style_Fill::FILL_SOLID, 
										"color"        => array("rgb" => "e2e2e2")), ) );
			$objPHPExcel->getActiveSheet()->setCellValue("B".($excel_row+2)," ".$A);
			$excel_row++;
			//excel
			
			$n=1;
			$j=1;
			while($j <= $num2[$i]){
				if($_POST["Q".$i."_".$j]=='' && $_POST["answer".$i."_".$j]==''){
					//delete---------------------------------------------------------------
					$oldlayer=$i*10000+$j*100;
					$query = "DELETE FROM drag_and_drop  
							WHERE layer=".$oldlayer." AND topicid=".$_SESSION['topic']." AND level=".$level;
					$result = mysql_query($query,$user_con);
					$query = "DELETE FROM dnd_answer 
							WHERE layer=".$oldlayer." ADN topicid=".$_SESSION['topic']." AND level=".$level;
					$result = mysql_query($query,$user_con);
				}
				if($_POST["Q".$i."_".$j]!='' && $_POST["Q".$i."_".$j]!=' '){
					$layer=$m*10000+$n*100;
					$oldlayer=$i*10000+$j*100;
					$Q = str_replace("'", "&#39;", $_POST["Q".$i."_".$j]);
					$Q = str_replace('"', '&quot;', $Q);
					$A = str_replace("'", "&#39;", $_POST["answer".$i."_".$j]);
					$A = str_replace('"', '&quot;', $A);
					$query = "UPDATE drag_and_drop  
							SET question='".$Q."',
								layer='".$layer."' 
							WHERE layer=".$oldlayer." AND topicid=".$_SESSION['topic']." AND level=".$level;
					$result = mysql_query($query,$user_con);
					$query = "UPDATE dnd_answer 
							SET answer='".$A."',
								layer='".$layer."' 
							WHERE layer=".$oldlayer." ADN topicid=".$_SESSION['topic']." AND level=".$level;
					$result = mysql_query($query,$user_con);
					
					//excel
					$objPHPExcel->getActiveSheet()->getStyle("A".($excel_row+2))->getFont()->setSize(16);
					$objPHPExcel->getActiveSheet()->getStyle("A".($excel_row+2))->applyFromArray( 
					array("fill"     => array("type"        => PHPExcel_Style_Fill::FILL_SOLID, 
												"color"        => array("rgb" => "BDEDFF")), ) );
					$Q = str_replace("&#39;", "'", $Q);
					$Q = str_replace('&quot;', '"', $Q);
					$objPHPExcel->getActiveSheet()->setCellValue("A".($excel_row+2),"     ".$Q);
			
					$A = str_replace("&#39;", "'", $A);
					$A = str_replace('&quot;', '"', $A);
					$objPHPExcel->getActiveSheet()->getStyle("B".($excel_row+2))->getFont()->setSize(14);
					$objPHPExcel->getActiveSheet()->getStyle("B".($excel_row+2))->applyFromArray( 
					array("fill"     => array("type"        => PHPExcel_Style_Fill::FILL_SOLID, 
												"color"        => array("rgb" => "e2e2e2")), ) );
					$objPHPExcel->getActiveSheet()->setCellValue("B".($excel_row+2)," ".$A);
					$excel_row++;
					//excel
					
					$p=1;
					$k=1;
					while($k <= $num3[$i][$j]){
						if($_POST["Q".$i."_".$j."_".$k]=='' && $_POST["answer".$i."_".$j."_".$k]==''){
							//delete---------------------------------------------------------------
							$oldlayer=$i*10000+$j*100+$k;
							$query = "DELETE FROM drag_and_drop  
									WHERE layer=".$oldlayer." AND topicid=".$_SESSION['topic']." AND level=".$level;
							$result = mysql_query($query,$user_con);
							$query = "DELETE FROM dnd_answer 
									WHERE layer=".$oldlayer." AND topicid=".$_SESSION['topic']." AND level=".$level;
							$result = mysql_query($query,$user_con);
						}
						if($_POST["Q".$i."_".$j."_".$k]!='' && $_POST["Q".$i."_".$j."_".$k]!=' '){
							$layer=$m*10000+$n*100+$p;
							$oldlayer=$i*10000+$j*100+$k;
							$Q = str_replace("'", "&#39;", $_POST["Q".$i."_".$j."_".$k]);
							$Q = str_replace('"', '&quot;', $Q);
							$A = str_replace("'", "&#39;", $_POST["answer".$i."_".$j."_".$k]);
							$A = str_replace('"', '&quot;', $A);
							$query = "UPDATE drag_and_drop  
									SET question='".$Q."',
										layer='".$layer."' 
									WHERE layer=".$oldlayer." AND topicid=".$_SESSION['topic']." AND level=".$level;
							$result = mysql_query($query,$user_con);
							$query = "UPDATE dnd_answer 
									SET answer='".$A."',
										layer='".$layer."' 
									WHERE layer=".$oldlayer." AND topicid=".$_SESSION['topic']." AND level=".$level;
							$result = mysql_query($query,$user_con);
							$p++;
							
							//excel
							$objPHPExcel->getActiveSheet()->getStyle("A".($excel_row+2))->getFont()->setSize(16);
							$objPHPExcel->getActiveSheet()->getStyle("A".($excel_row+2))->applyFromArray( 
							array("fill"     => array("type"        => PHPExcel_Style_Fill::FILL_SOLID, 
														"color"        => array("rgb" => "E0FFFF")), ) );
							$Q = str_replace("&#39;", "'", $Q);
							$Q = str_replace('&quot;', '"', $Q);
							$objPHPExcel->getActiveSheet()->setCellValue("A".($excel_row+2),"           ".$Q);
			
							$A = str_replace("&#39;", "'", $A);
							$A = str_replace('&quot;', '"', $A);
							$objPHPExcel->getActiveSheet()->getStyle("B".($excel_row+2))->getFont()->setSize(14);
							$objPHPExcel->getActiveSheet()->getStyle("B".($excel_row+2))->applyFromArray( 
							array("fill"     => array("type"        => PHPExcel_Style_Fill::FILL_SOLID, 
														"color"        => array("rgb" => "e2e2e2")), ) );
							$objPHPExcel->getActiveSheet()->setCellValue("B".($excel_row+2)," ".$A);
							$excel_row++;
							//excel
							
						}
						$k++;
					}
					while($k<=$num3[$i][$j]+$DND3[$i][$j]){
						if($_POST["Q".$i."_".$j."_".$k]!='' && $_POST["Q".$i."_".$j."_".$k]!=' '){
							$layer=$m*10000+$n*100+$p;
							$Q = str_replace("'", "&#39;", $_POST["Q".$i."_".$j."_".$k]);
							$Q = str_replace('"', '&quot;', $Q);
							$A = str_replace("'", "&#39;", $_POST["answer".$i."_".$j."_".$k]);
							$A = str_replace('"', '&quot;', $A);
							$query = "INSERT INTO drag_and_drop (question, topicid, level, layer) 
										VALUES('".$Q."',
												".$_SESSION['topic'].",
												".$level.",
											   	".$layer.")";
							$result = mysql_query($query,$user_con);
							$query = "INSERT INTO dnd_answer (answer, topicid, level, layer) 
										VALUES('".$A."',
												".$_SESSION['topic'].",
												".$level.",
											   	".$layer.")";
							$result = mysql_query($query,$user_con);
							$p++;
							
							//excel
							$objPHPExcel->getActiveSheet()->getStyle("A".($excel_row+2))->getFont()->setSize(16);
							$objPHPExcel->getActiveSheet()->getStyle("A".($excel_row+2))->applyFromArray( 
							array("fill"     => array("type"        => PHPExcel_Style_Fill::FILL_SOLID, 
														"color"        => array("rgb" => "E0FFFF")), ) );
							$Q = str_replace("&#39;", "'", $Q);
							$Q = str_replace('&quot;', '"', $Q);
							$objPHPExcel->getActiveSheet()->setCellValue("A".($excel_row+2),"           ".$Q);
			
							$A = str_replace("&#39;", "'", $A);
							$A = str_replace('&quot;', '"', $A);
							$objPHPExcel->getActiveSheet()->getStyle("B".($excel_row+2))->getFont()->setSize(14);
							$objPHPExcel->getActiveSheet()->getStyle("B".($excel_row+2))->applyFromArray( 
							array("fill"     => array("type"        => PHPExcel_Style_Fill::FILL_SOLID, 
														"color"        => array("rgb" => "e2e2e2")), ) );
							$objPHPExcel->getActiveSheet()->setCellValue("B".($excel_row+2)," ".$A);
							$excel_row++;
							//excel
							
						}
						$k++;
					}
					$n++;
				}
				$j++;	
			}			
			while($j<=$num2[$i]+$DND2[$i]){
				if($_POST["Q".$i."_".$j]!='' && $_POST["Q".$i."_".$j]!=' '){
					$layer=$m*10000+$n*100;
					$Q = str_replace("'", "&#39;", $_POST["Q".$i."_".$j]);
					$Q = str_replace('"', '&quot;', $Q);
					$A = str_replace("'", "&#39;", $_POST["answer".$i."_".$j]);
					$A = str_replace('"', '&quot;', $A);
					$query = "INSERT INTO drag_and_drop (question, topicid, level, layer) 
							VALUES('".$Q."',
									".$_SESSION['topic'].",
									".$level.",
									".$layer.")";
					$result = mysql_query($query,$user_con);
					$query = "INSERT INTO dnd_answer (answer, topicid, level, layer) 
							VALUES('".$A."',
									".$_SESSION['topic'].",
									".$level.",
									".$layer.")";
					$result = mysql_query($query,$user_con);
					
					//excel
					$objPHPExcel->getActiveSheet()->getStyle("A".($excel_row+2))->getFont()->setSize(16);
					$objPHPExcel->getActiveSheet()->getStyle("A".($excel_row+2))->applyFromArray( 
					array("fill"     => array("type"        => PHPExcel_Style_Fill::FILL_SOLID, 
												"color"        => array("rgb" => "BDEDFF")), ) );
					$Q = str_replace("&#39;", "'", $Q);
					$Q = str_replace('&quot;', '"', $Q);
					$objPHPExcel->getActiveSheet()->setCellValue("A".($excel_row+2),"     ".$Q);
			
					$A = str_replace("&#39;", "'", $A);
					$A = str_replace('&quot;', '"', $A);
					$objPHPExcel->getActiveSheet()->getStyle("B".($excel_row+2))->getFont()->setSize(14);
					$objPHPExcel->getActiveSheet()->getStyle("B".($excel_row+2))->applyFromArray( 
					array("fill"     => array("type"        => PHPExcel_Style_Fill::FILL_SOLID, 
												"color"        => array("rgb" => "e2e2e2")), ) );
					$objPHPExcel->getActiveSheet()->setCellValue("B".($excel_row+2)," ".$A);
					$excel_row++;
					//excel
					
					$p=1;
					$k=1;
					while($k<=$DND3[$i][$j]){
						if($_POST["Q".$i."_".$j."_".$k]!='' && $_POST["Q".$i."_".$j."_".$k]!=' '){
							$layer=$m*10000+$n*100+$p;
							$Q = str_replace("'", "&#39;", $_POST["Q".$i."_".$j."_".$k]);
							$Q = str_replace('"', '&quot;', $Q);
							$A = str_replace("'", "&#39;", $_POST["answer".$i."_".$j."_".$k]);
							$A = str_replace('"', '&quot;', $A);
							$query = "INSERT INTO drag_and_drop (question, topicid, level, layer) 
								VALUES('".$Q."',
										".$_SESSION['topic'].",
										".$level.",
										".$layer.")";
							$result = mysql_query($query,$user_con);
							$query = "INSERT INTO dnd_answer (answer, topicid, level, layer) 
										VALUES('".$A."',
												".$_SESSION['topic'].",
												".$level.",
											   	".$layer.")";
							$result = mysql_query($query,$user_con);
							$p++;
							
							//excel
							$objPHPExcel->getActiveSheet()->getStyle("A".($excel_row+2))->getFont()->setSize(16);
							$objPHPExcel->getActiveSheet()->getStyle("A".($excel_row+2))->applyFromArray( 
							array("fill"     => array("type"        => PHPExcel_Style_Fill::FILL_SOLID, 
														"color"        => array("rgb" => "E0FFFF")), ) );
							$Q = str_replace("&#39;", "'", $Q);
							$Q = str_replace('&quot;', '"', $Q);
							$objPHPExcel->getActiveSheet()->setCellValue("A".($excel_row+2),"           ".$Q);
			
							$A = str_replace("&#39;", "'", $A);
							$A = str_replace('&quot;', '"', $A);
							$objPHPExcel->getActiveSheet()->getStyle("B".($excel_row+2))->getFont()->setSize(14);
							$objPHPExcel->getActiveSheet()->getStyle("B".($excel_row+2))->applyFromArray( 
							array("fill"     => array("type"        => PHPExcel_Style_Fill::FILL_SOLID, 
														"color"        => array("rgb" => "e2e2e2")), ) );
							$objPHPExcel->getActiveSheet()->setCellValue("B".($excel_row+2)," ".$A);
							$excel_row++;
							//excel
							
						}
						$k++;
					}
					$n++;
				}
				$j++;	
			}		
			$m++;
		}
		$i++;
	}
	$i=$num1+1;
	while($i <= $DND1+$num1)
	{	
		if($_POST['Q'.$i]!='' && $_POST['Q'.$i]!=' '){
			$layer=$m*10000;
			$Q = str_replace("'", "&#39;", $_POST['Q'.$i]);
			$Q = str_replace('"', '&quot;', $Q);
			$A = str_replace("'", "&#39;", $_POST['answer'.$i]);
			$A = str_replace('"', '&quot;', $A);
			
			
			$query = "INSERT INTO drag_and_drop (question, layer, topicid, level) 
				VALUES ('".$Q."',
							'".$layer."', 
							".$_SESSION['topic'].", ".$level.")";
			$result = mysql_query($query,$user_con);
			$query = "INSERT INTO dnd_answer (answer, layer, topicid, level) 
				VALUES ('".$A."',
							'".$layer."', 
							".$_SESSION['topic'].", ".$level.")";
			$result = mysql_query($query,$user_con);
			
			//excel
			$objPHPExcel->getActiveSheet()->getStyle("A".($excel_row+2))->getFont()->setSize(16);
			$objPHPExcel->getActiveSheet()->getStyle("A".($excel_row+2))->applyFromArray( 
			array("fill"     => array("type"        => PHPExcel_Style_Fill::FILL_SOLID, 
										"color"        => array("rgb" => "ADDFFF")), ) );
			$Q = str_replace("&#39;", "'", $Q);
			$Q = str_replace('&quot;', '"', $Q);
			$objPHPExcel->getActiveSheet()->setCellValue("A".($excel_row+2)," ".$Q);
			
			$A = str_replace("&#39;", "'", $A);
			$A = str_replace('&quot;', '"', $A);
			$objPHPExcel->getActiveSheet()->getStyle("B".($excel_row+2))->getFont()->setSize(14);
			$objPHPExcel->getActiveSheet()->getStyle("B".($excel_row+2))->applyFromArray( 
			array("fill"     => array("type"        => PHPExcel_Style_Fill::FILL_SOLID, 
										"color"        => array("rgb" => "e2e2e2")), ) );
			$objPHPExcel->getActiveSheet()->setCellValue("B".($excel_row+2)," ".$A);
			$excel_row++;
			//excel
			
			$n=1;
			$j=1;
			while($j <= $DND2[$i]){
				if($_POST["Q".$i."_".$j]!='' && $_POST["Q".$i."_".$j]!=' '){
					$layer=$m*10000+$n*100;
					$Q = str_replace("'", "&#39;", $_POST["Q".$i."_".$j]);
					$Q = str_replace('"', '&quot;', $Q);
					$A = str_replace("'", "&#39;", $_POST["answer".$i."_".$j]);
					$A = str_replace('"', '&quot;', $A);
					$query = "INSERT INTO drag_and_drop (question, layer, topicid, level) 
								VALUES ('".$Q."',
								'".$layer."', 
								".$_SESSION['topic'].", ".$level.")";
					$result = mysql_query($query,$user_con);
					$query = "INSERT INTO dnd_answer (answer, layer, topicid, level) 
								VALUES ('".$A."',
								'".$layer."', 
								".$_SESSION['topic'].", ".$level.")";
					$result = mysql_query($query,$user_con);
					
					//excel
					$objPHPExcel->getActiveSheet()->getStyle("A".($excel_row+2))->getFont()->setSize(16);
					$objPHPExcel->getActiveSheet()->getStyle("A".($excel_row+2))->applyFromArray( 
					array("fill"     => array("type"        => PHPExcel_Style_Fill::FILL_SOLID, 
												"color"        => array("rgb" => "BDEDFF")), ) );
					$Q = str_replace("&#39;", "'", $Q);
					$Q = str_replace('&quot;', '"', $Q);
					$objPHPExcel->getActiveSheet()->setCellValue("A".($excel_row+2),"     ".$Q);
			
					$A = str_replace("&#39;", "'", $A);
					$A = str_replace('&quot;', '"', $A);
					$objPHPExcel->getActiveSheet()->getStyle("B".($excel_row+2))->getFont()->setSize(14);
					$objPHPExcel->getActiveSheet()->getStyle("B".($excel_row+2))->applyFromArray( 
					array("fill"     => array("type"        => PHPExcel_Style_Fill::FILL_SOLID, 
												"color"        => array("rgb" => "e2e2e2")), ) );
					$objPHPExcel->getActiveSheet()->setCellValue("B".($excel_row+2)," ".$A);
					$excel_row++;
					//excel
					
					$p=1;
					$k=1;
					while($k <= $DND3[$i][$j]){
						if($_POST["Q".$i."_".$j."_".$k]!='' && $_POST["Q".$i."_".$j."_".$k]!=' '){
							$layer=$m*10000+$n*100+$p;
							$Q = str_replace("'", "&#39;", $_POST["Q".$i."_".$j."_".$k]);
							$Q = str_replace('"', '&quot;', $Q);
							$A = str_replace("'", "&#39;", $_POST["answer".$i."_".$j."_".$k]);
							$A = str_replace('"', '&quot;', $A);
							$query = "INSERT INTO drag_and_drop (question, layer, topicid, level) 
									VALUES ('".$Q."',
									'".$layer."', 
									".$_SESSION['topic'].", ".$level.")";
							$result = mysql_query($query,$user_con);
							$query = "INSERT INTO dnd_answer (answer, layer, topicid, level) 
									VALUES ('".$A."',
									'".$layer."', 
									".$_SESSION['topic'].", ".$level.")";
							$result = mysql_query($query,$user_con);
							$p++;
							
							//excel
							$objPHPExcel->getActiveSheet()->getStyle("A".($excel_row+2))->getFont()->setSize(16);
							$objPHPExcel->getActiveSheet()->getStyle("A".($excel_row+2))->applyFromArray( 
							array("fill"     => array("type"        => PHPExcel_Style_Fill::FILL_SOLID, 
														"color"        => array("rgb" => "E0FFFF")), ) );
							$Q = str_replace("&#39;", "'", $Q);
							$Q = str_replace('&quot;', '"', $Q);
							$objPHPExcel->getActiveSheet()->setCellValue("A".($excel_row+2),"           ".$Q);
			
							$A = str_replace("&#39;", "'", $A);
							$A = str_replace('&quot;', '"', $A);
							$objPHPExcel->getActiveSheet()->getStyle("B".($excel_row+2))->getFont()->setSize(14);
							$objPHPExcel->getActiveSheet()->getStyle("B".($excel_row+2))->applyFromArray( 
							array("fill"     => array("type"        => PHPExcel_Style_Fill::FILL_SOLID, 
														"color"        => array("rgb" => "e2e2e2")), ) );
							$objPHPExcel->getActiveSheet()->setCellValue("B".($excel_row+2)," ".$A);
							$excel_row++;
							//excel
							
						}
						$k++;
					}
					$n++;
				}
				$j++;	
			}
			$m++;
		}
		$i++;
	}
	//excel
	$sql = "SELECT name FROM `topic` WHERE `topicid` = '".$_SESSION['topic']."'";
	$result = mysql_query($sql, $user_con);
	$tname = mysql_fetch_row($result);
	$filename = "../../dnd_answer/".$tname[0]."_".$_SESSION['topic']."_".$level.".xls";
	
	$objPHPExcel->getActiveSheet()->getColumnDimension("A")->setAutoSize(true);
	$objPHPExcel->getActiveSheet()->getColumnDimension("B")->setAutoSize(true);
	$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, "Excel5"); 
	$objWriter->save($filename);
	//excel
	
	$i=1;
	while($i<=$fake_num){
		if($_POST['fake'.$i]==''){
				$layer=0;
				$query = "DELETE FROM dnd_answer 
							WHERE ansid=".$_POST['ansid'.$i]." AND layer=".$layer." AND topicid=".$_SESSION['topic']." AND level=".$level;
				//echo $query."<br>";
				$result = mysql_query($query,$user_con);
		}
		if($_POST['fake'.$i]!='' && $_POST['fake'.$i]!=' '){
				$layer=0;
				$f_ans = str_replace("'", "&#39;", $_POST['fake'.$i]);
				$f_ans = str_replace('"', '&quot;', $f_ans);
				$query = "UPDATE dnd_answer 
							SET answer='".$f_ans."' 
							WHERE ansid=".$_POST['ansid'.$i]." AND layer=".$layer." AND topicid=".$_SESSION['topic']." AND level=".$level;
				//echo $query."<br>";
				$result = mysql_query($query,$user_con);
		}
		$i++;
	}
	while($i<=$FAKE+$fake_num){
		if($_POST['fake'.$i]!='' && $_POST['fake'.$i]!=' '){
				$layer=0;
				$f_ans = str_replace("'", "&#39;", $_POST['fake'.$i]);
				$f_ans = str_replace('"', '&quot;', $f_ans);
				$query = "INSERT INTO dnd_answer (answer, layer, topicid, level) 
							VALUES ('".$f_ans."',
							'".$layer."', 
							".$_SESSION['topic'].", ".$level.")";
				$result = mysql_query($query,$user_con);
		}
		$i++;
	}
	header("location: page.php");	
}
//value initialization
if(!isset($_POST['DND1'])){
			$flag=1;
			$DND1=0;
			$FAKE=0;
}
else{
	$flag=0;
	$FAKE=$_POST['FAKE'];
	if($_POST['submit']=="add a fake answer"){
		$FAKE++;
	}
	else if($_POST['submit']=="add layer 1"){
			$DND1++;		
			$DND2[$DND1+$num1]=0;
			$DND3[$DND1+$num1][1]=0;
	}
	else{
		for($i=1; $i<=$DND1+$num1; $i++){
			$str="add layer 2 to T".$i;
			//echo $_POST['submit']."<br>";
			if($_POST['submit']==$str){
				//echo "DND2_".$i."=".$DND2[$i]."<br>";
				$DND2[$i]++;
				$DND3[$i][$DND2[$i]]=0;
				break;
			}
			for($j=1; $j<=$DND2[$i]+$num2[$i]; $j++){
				$str="add layer 3 to T".$i."-".$j;
				if($_POST['submit']==$str){
					//echo "DND3_".$i."_".$j."=".$DND3[$i][$j]."<br>";
					$DND3[$i][$j]++;
					break;
				}
			}
		}
	}
}
?>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="../framestyle.css" type="text/css" media="screen">
<style type="text/css">
</style>
<script>
</script>
</head>

<body>
<div id="wrapper">
<div id="title">Online Listening Comprehension Program</div>

<div id="main">
<hr/>
<div id="title2">FOR INTERMEDIATE LEVEL: DRAG AND DROP</div>
<hr/><br><br>
<form method = POST action="drag_and_drop2.php" >

<?php 



if($flag==1){
	
	
	$query = "SELECT * FROM drag_and_drop WHERE topicid=".$_SESSION['topic']." AND level=".$level." ORDER BY layer";
	$result = mysql_query($query,$user_con);
	$cnt=0;
	$num1=0;
	$DND1=0;
	echo '<input type="hidden" name="DND1" value='.$DND1.'>';
	while($row[$cnt] = mysql_fetch_array($result)){
		$query2 = "SELECT * FROM dnd_answer WHERE topicid=".$_SESSION['topic']." AND level=".$level." AND layer=".$row[$cnt]['layer'];
		$result2 = mysql_query($query2);
		$row2=mysql_fetch_array($result2);
		$Q = str_replace("'", "&#39;", $row[$cnt]['question']);
		$Q = str_replace('"', '&quot;', $Q);
		$A = str_replace("'", "&#39;", $row2['answer']);
		$A = str_replace('"', '&quot;', $A);
		if($row[$cnt]['layer']==10000){
			$num1++;
			
			echo '<div class="layer first">
				T'.$num1.': <input class="area long" type="text" name="Q'.$num1.'" value="'.$Q.'"><br>
				answer: <input class="area long" type="text" name="answer'.$num1.'" value="'.$A.'"><br>
				<input type="submit" name = "submit" value = "add layer 2 to T'.$num1.'"></div>';
			$num2[$num1]=0;
			$DND2[$num1]=0;
			echo "<input type='hidden' name='DND2_".$num1."' value=".$DND2[$num1].">";
		}	
		if((int)($row[$cnt]['layer']/10000)!=1 && (int)($row[$cnt]['layer']/10000)!=(int)($row[$cnt-1]['layer']/10000)){
			$num1++;
			echo '<br><br><div class="layer first">
				T'.$num1.': <input class="area long" type="text" name="Q'.$num1.'" value="'.$Q.'"><br>
				answer: <input class="area long" type="text" name="answer'.$num1.'" value="'.$A.'"><br>
				<input type="submit" name = "submit" value = "add layer 2 to T'.$num1.'"></div>';
			$num2[$num1]=0;
			$DND2[$num1]=0;
			echo "<input type='hidden' name='DND2_".$num1."' value=".$DND2[$num1].">";
		}
		else if($row[$cnt]['layer']!=10000 && (int)($row[$cnt]['layer']/10000)==(int)($row[$cnt-1]['layer']/10000)){
			if((int)($row[$cnt]['layer']/100)!=1 && (int)($row[$cnt]['layer']/100)!=(int)($row[$cnt-1]['layer']/100)){
				$num2[$num1]++;
				echo '<div class="layer second">
					T'.$num1.'-'.$num2[$num1].' : <input class="area medium" type="text" name="Q'.$num1.'_'.$num2[$num1].'" value="'.$Q.'"><br>
					answer: <input class="area medium" type="text" name="answer'.$num1.'_'.$num2[$num1].'" value="'.$A.'"><br>
					<input type="submit" name = "submit" value = "add layer 3 to T'.$num1.'-'.$num2[$num1].'"></div>';
				$num3[$num1][$num2[$num1]]=0;
				$DND3[$num1][$num2[$num1]]=0;
				echo '<input type="hidden" name="DND3_'.$num1.'_'.$num2[$num1].'" value='.$DND3[$num1][$num2[$num1]].'>';
			}	
			else{
				$num3[$num1][$num2[$num1]]++;
				echo '<div class="layer third">
					T'.$num1.'-'.$num2[$num1].'-'.$num3[$num1][$num2[$num1]].': <input class="area short" type="text" name="Q'.$num1.'_'.$num2[$num1].'_'.$num3[$num1][$num2[$num1]].'" 
					value="'.$Q.'"><br>
					answer: <input class="area short" type="text" name="answer'.$num1.'_'.$num2[$num1].'_'.$num3[$num1][$num2[$num1]].'" value="'.$A.'"><br><br></div>';
			}
		}
		$cnt++;
	}
}else{
	$i=1;
	while($i<=$num1){
		$j=1;
		while($j<=$num2[$i]){
			echo '<input type="hidden" name="num3_'.$i.'_'.$j.'" value='.$num3[$i][$j].'>';
			$j++;
		}
		$Q = str_replace("'", "&#39;", $_POST['Q'.$i]);
		$Q = str_replace('"', '&quot;', $Q);
		$A = str_replace("'", "&#39;", $_POST['answer'.$i]);
		$A = str_replace('"', '&quot;', $A);
		echo '<div class="layer first">
			T'.$i.': <input class="area long" type="text" name="Q'.$i.'" value="'.$Q.'"><br>
			answer: <input class="area long" type="text" name="answer'.$i.'" value="'.$A.'"><br>
			<input type="hidden" name="DND1" value='.$num1.'>
			<input type="hidden" name="DND2_'.$i.'" value='.$num2[$i].'>
			<input type="submit" name = "submit" value = "add layer 2 to T'.$i.'"></div>';
		$j=1;
		while($j<=$num2[$i]){
			$Q = str_replace("'", "&#39;", $_POST["Q".$i."_".$j]);
			$Q = str_replace('"', '&quot;', $Q);
			$A = str_replace("'", "&#39;", $_POST["answer".$i."_".$j]);
			$A = str_replace('"', '&quot;', $A);
			echo '<div class="layer second">
				T'.$i.'-'.$j.' : <input class="area medium" type="text" name="Q'.$i.'_'.$j.'" value="'.$Q.'"><br>
				answer: <input class="area medium" type="text" name="answer'.$i.'_'.$j.'" value="'.$A.'"><br>
				<input type="submit" name = "submit" value = "add layer 3 to T'.$i.'-'.$j.'"></div>';
			$k=1;
			while($k<=$num3[$i][$j]){
				$Q = str_replace("'", "&#39;", $_POST["Q".$i."_".$j."_".$k]);
				$Q = str_replace('"', '&quot;', $Q);
				$A = str_replace("'", "&#39;", $_POST["answer".$i."_".$j."_".$k]);
				$A = str_replace('"', '&quot;', $A);
				echo '<div class="layer third">
					T'.$i.'-'.$j.'-'.$k.': <input class="area short" type="text" name="Q'.$i.'_'.$j.'_'.$k.'" 
					value="'.$Q.'"><br>
					answer: <input class="area short" type="text" name="answer'.$i.'_'.$j.'_'.$k.'" value="'.$A.'"><br><br></div>';
				$k++;
			}
			//
			while($k<=$num3[$i][$j]+$DND3[$i][$j]){
				$Q = str_replace("'", "&#39;", $_POST["Q".$i."_".$j."_".$k]);
				$Q = str_replace('"', '&quot;', $Q);
				$A = str_replace("'", "&#39;", $_POST["answer".$i."_".$j."_".$k]);
				$A = str_replace('"', '&quot;', $A);
				echo '<div class="layer third">
					T'.$i.'-'.$j.'-'.$k.': <input class="area short" type="text" name="Q'.$i.'_'.$j.'_'.$k.'" 
					value="'.$Q.'"><br>
					answer: <input class="area short" type="text" name="answer'.$i.'_'.$j.'_'.$k.'" value="'.$A.'"><br><br></div>';
				$k++;
			}
			$j++;	
		}	
		while($j<=$num2[$i]+$DND2[$i]){
			$Q = str_replace("'", "&#39;", $_POST["Q".$i."_".$j]);
			$Q = str_replace('"', '&quot;', $Q);
			$A = str_replace("'", "&#39;", $_POST["answer".$i."_".$j]);
			$A = str_replace('"', '&quot;', $A);
			echo '<div class="layer second">
				T'.$i.'-'.$j.' : <input class="area medium" type="text" name="Q'.$i.'_'.$j.'" value="'.$Q.'"><br>
				answer: <input class="area medium" type="text" name="answer'.$i.'_'.$j.'" value="'.$A.'"><br>
				<input type="submit" name = "submit" value = "add layer 3 to T'.$i.'-'.$j.'"></div>';
			$k=1;
			while($k<=$DND3[$i][$j]){
				$Q = str_replace("'", "&#39;", $_POST["Q".$i."_".$j."_".$k]);
				$Q = str_replace('"', '&quot;', $Q);
				$A = str_replace("'", "&#39;", $_POST["answer".$i."_".$j."_".$k]);
				$A = str_replace('"', '&quot;', $A);
				echo '<div class="layer third">
					T'.$i.'-'.$j.'-'.$k.': <input class="area short" type="text" name="Q'.$i.'_'.$j.'_'.$k.'" 
					value="'.$Q.'"><br>
					answer: <input class="area short" type="text" name="answer'.$i.'_'.$j.'_'.$k.'" value="'.$A.'"><br><br></div>';
				$k++;
			}
			$j++;	
		}	
		$i++;
		echo "<br><br>";
	}
}
echo "<input type='hidden' name='num1' value=".$num1.">";	
$i=1;
while($i<=$num1){
	echo "<input type='hidden' name='num2_".$i."' value=".$num2[$i].">";
	echo "<input type='hidden' name='DND2_".$i."' value=".$DND2[$i].">";
	$j=1;
	while($j<=$num2[$i]+$DND2[$i]){
		echo "<input type='hidden' name='num3_".$i."_".$j."' value=".$num3[$i][$j].">";	
		echo "<input type='hidden' name='DND3_".$i."_".$j."' value=".$DND3[$i][$j].">";
		$j++;
	}
	$i++;
}

?>

<?php 
$i=$num1+1;

echo '<input type="hidden" name="DND1" value='.$DND1.'>';
while($i<=$DND1+$num1){
	$j=1;
	while($j<=$DND2[$i]){
		echo '<input type="hidden" name="DND3_'.$i.'_'.$j.'" value='.$DND3[$i][$j].'>';
		$j++;
	}
	$Q = str_replace("'", "&#39;", $_POST['Q'.$i]);
	$Q = str_replace('"', '&quot;', $Q);
	$A = str_replace("'", "&#39;", $_POST['answer'.$i]);
	$A = str_replace('"', '&quot;', $A);
	echo '<div class="layer first">
	T'.$i.': <input class="area long" type="text" name="Q'.$i.'" value="'.$Q.'"><br>
	answer: <input class="area long" type="text" name="answer'.$i.'" value="'.$A.'"><br>
	<input type="hidden" name="DND1" value='.$DND1.'>
	<input type="hidden" name="DND2_'.$i.'" value='.$DND2[$i].'>
	<input type="submit" name = "submit" value = "add layer 2 to T'.$i.'"></div>';
	$j=1;
	while($j<=$DND2[$i]){
		$Q = str_replace("'", "&#39;", $_POST["Q".$i."_".$j]);
		$Q = str_replace('"', '&quot;', $Q);
		$A = str_replace("'", "&#39;", $_POST["answer".$i."_".$j]);
		$A = str_replace('"', '&quot;', $A);
		echo '<div class="layer second">
			T'.$i.'-'.$j.' : <input class="area medium" type="text" name="Q'.$i.'_'.$j.'" value="'.$Q.'"><br>
			answer: <input class="area medium" type="text" name="answer'.$i.'_'.$j.'" value="'.$A.'"><br>
			<input type="submit" name = "submit" value = "add layer 3 to T'.$i.'-'.$j.'"></div>';
		$k=1;
		while($k<=$DND3[$i][$j]){
			$Q = str_replace("'", "&#39;", $_POST["Q".$i."_".$j."_".$k]);
			$Q = str_replace('"', '&quot;', $Q);
			$A = str_replace("'", "&#39;", $_POST["answer".$i."_".$j."_".$k]);
			$A = str_replace('"', '&quot;', $A);
			echo '<div class="layer third">
				T'.$i.'-'.$j.'-'.$k.': <input class="area short" type="text" name="Q'.$i.'_'.$j.'_'.$k.'" 
				value="'.$Q.'"><br>
				answer: <input class="area short" type="text" name="answer'.$i.'_'.$j.'_'.$k.'" value="'.$A.'"><br><br></div>';
			$k++;
		}
		$j++;	
	}	
	$i++;
	echo "<br><br>";
}
if($DND1==0)
	echo "<br><br>";
?>
<div class="btn_right"><input type="submit" name = "submit" value = "add layer 1"></div><br><br><br>
<?php
$query = "SELECT * FROM dnd_answer WHERE topicid=".$_SESSION['topic']." AND level=".$level." AND layer=0";
$result = mysql_query($query,$user_con);
$i=1;
while($row=mysql_fetch_array($result)){
	if($flag==1)
		$fake_ans=$row['answer'];
	else
		$fake_ans=$_POST['fake'.$i];
	$fake_ans = str_replace("'", "&#39;", $fake_ans);
	$fake_ans = str_replace('"', '&quot;', $fake_ans);
	echo "<div class='layer fake'>";
	echo 'fake ans: <td><input class="area long" type="text" name="fake'.$i.'" value="'.$fake_ans.'">';	
	echo "</div><br>";
	echo "<input type='hidden' name='ansid".$i."' value=".$row['ansid'].">";
	$i++;
}
$fake_num=$i-1;
while($i<=$FAKE+$fake_num){
	$fake_ans = str_replace("'", "&#39;", $_POST['fake'.$i]);
	$fake_ans = str_replace('"', '&quot;', $fake_ans);
	echo "<div class='layer fake'>";
	echo 'fake ans: <td><input class="area long" type="text" name="fake'.$i.'" value="'.$fake_ans.'">';	
	echo "</div><br>";
	$i++;
}
echo '<div class="btn_right"><input type="submit" name = "submit" value = "add a fake answer"></div>';
echo "<input type='hidden' name='FAKE' value=".$FAKE.">";
echo "<input type='hidden' name='fake_num' value=".$fake_num.">";


?>


<br><br>
<div class="btn_right"><input type="submit" name = "submit" value = "submit"></div>
</div>
</form>

</body>

